<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 12:26
 */

/**
 * Class Involved_SupplierModel
 */

class Involved_SupplierModel extends BaseModel{

    protected $table = 'supplier';


    /******************   App接口   *********************/

    /**
     * 获取商家信息
     * @param $user_id
     * @return mixed
     * @author fyl
     * @date 2016-2-17
     */
    public function GetSupplierInfo($user_id)
    {
        $supplier_info =  DB::table($this->table)
            ->select('supplier_id','company','brand','scale','feature','phone','brief',
                     'market','supplier_name','show_img1','show_img2','show_img3','bank_account','bank_number')
            ->where('supplier_id',$user_id)
            ->first();

        return $supplier_info;
    }

    /**
     * 获取商家详情信息
     * @param $supplier_id
     * @return mixed
     * @author alice
     * @date 2016-1-26
     */
    public function GetSupplierDetail($supplier_id)
    {
        return  DB::table($this->table)
                ->select('supplier_id','supplier_name','brand','scale','business_scope',
                         'storage','market','show_img1','show_img2','show_img3')
                ->where(['supplier_id'=>$supplier_id])
                ->first();
    }


    /**
     * 获取商家产品图片
     * @return
     */
    public function GetSupplier($supplier_id)
    {
        $info =  DB::table($this->table)
                ->select('supplier_name')
                ->where(['supplier_id'=>$supplier_id])
                ->first();
        $data['supplier_name']  = $info->supplier_name;
        $this->table='product';
        $info_products =  DB::table($this->table) ->where(['supplier_id'=>$supplier_id])
            ->select('thumb_img1')
            ->get();
        foreach($info_products as $k=>$v){
            $data['product_img'][]=$v;
        }
        return $data;
    }


    /**
     * 获取完善信息
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-1-24
     */
    public function GetCommonInfo($user_id)
    {
        return DB::table($this->table)
            ->select('supplier_id','company','supplier_name','address_all','operate_license',
                     'phone','address','show_img1','show_img2','show_img3')
            ->where('supplier_id',$user_id)
            ->first();
    }

    /**
     * 获取资质审核
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-1-24
     */
    public function GetCertifyInfo($user_id)
    {
        return DB::table($this->table)
            ->select('market','bank_account','bank_number','storage','id_card',
                'operate_license','tax_license')
            ->where('supplier_id',$user_id)
            ->first();
    }

    /**
     * 更新资质信息
     * @param $data
     * @param $supplier_id
     * @return mixed
     */
    public function AddVerify($data, $supplier_id)
    {
        $count = DB::table($this->table)->where('supplier_id',$supplier_id)->count();
        if($count){
           return  DB::table($this->table)->where(['supplier_id'=> $supplier_id])->update($data);
        }else{
            $data['supplier_id'] = $supplier_id;
           return DB::table($this->table)->insert($data);
        }
    }

    /******************   PC端   *********************/

    /**
     * 商家列表
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowSupplierList()
    {
        $info = DB::table($this->table)->paginate(8);
        foreach($info as $k => $v)
        {
            $v->created = date('Y-m-d');
        }
        return $info;
    }

    /**
     * 商家详情
     * @param $id
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowSupplierInfo($id)
    {
        return DB::table($this->table)->where('supplier_id',$id)->first();
    }

    /**
     * 根据ID获取供应商名
     * @param $supplier_id
     * @return mixed
     */
    public function GetSupplierNameById($supplier_id)
    {
        $supplier = DB::table($this->table)
                  ->select('supplier_name')
                  ->where(['supplier_id' => $supplier_id])
                  ->first();
        return $supplier->supplier_name;
    }
    /**
     * 根据ID获取公司名
     * @author fyl
     * @date 2016-5-7
     * @return mixed
     */
    public function ShowSupplierCompany($supplier_id)
    {
        return DB::table($this->table)->select('company')->where('supplier_id',$supplier_id)->first();
    }

}